1
|
|
|
Admin.Modules.add('display.datatables', () => { |
|
|
|
|
2
|
|
|
$.fn.dataTable.ext.errMode = () => { |
3
|
|
|
Admin.Messages.error( |
|
|
|
|
4
|
|
|
i18next.t('lang.table.error') |
|
|
|
|
5
|
|
|
) |
6
|
|
|
}; |
7
|
|
|
|
8
|
|
|
$.fn.dataTable.ext.order['DateTime'] = function (settings, col) { |
9
|
|
|
return this.api().column(col, {order: 'index'}).nodes().map((td, i) => { |
|
|
|
|
10
|
|
|
return $(td).data('value'); |
11
|
|
|
}); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
$('.datatables').each((i, item) => { |
15
|
|
|
var $this = $(item), |
16
|
|
|
id = $this.data('id'); |
17
|
|
|
|
18
|
|
|
var params = $this.data('attributes'); |
19
|
|
|
|
20
|
|
|
var url; |
21
|
|
|
if (url = $this.data('url')) { |
22
|
|
|
params.serverSide = true; |
23
|
|
|
params.processing = true; |
24
|
|
|
params.ajax = { |
25
|
|
|
url: url, |
26
|
|
|
data (d) { |
27
|
|
|
$('[data-datatables-id="' + id + '"] .column-filter[data-type]').each((i, subitem) => { |
28
|
|
|
var $this = $(subitem); |
29
|
|
|
var index = $this.closest('td').data('index'); |
30
|
|
|
if (name = $this.data('ajax-data-name')) { |
|
|
|
|
31
|
|
|
d.columns[index]['search'][name] = $this.val(); |
32
|
|
|
} |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
}; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
var table = $this.DataTable(params); |
39
|
|
|
|
40
|
|
|
$('[data-datatables-id="' + id + '"] .column-filter[data-type]').each((i, item) => { |
41
|
|
|
var $this = $(item), |
42
|
|
|
type = $this.data('type'), |
43
|
|
|
index = $this.closest('td').data('index'); |
44
|
|
|
|
45
|
|
|
if (_.isFunction(window.columnFilters[type])) { |
|
|
|
|
46
|
|
|
window.columnFilters[type](item, table.api(), table.api().column(index), index); |
47
|
|
|
} |
48
|
|
|
}); |
49
|
|
|
}); |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
window.columnFilters = { |
53
|
|
|
range (container, table, column, index) { |
54
|
|
|
let $container = $(container), |
55
|
|
|
from = $('input:first', $container), |
56
|
|
|
to = $('input:last', $container); |
57
|
|
|
|
58
|
|
|
var isDateRange = false; |
59
|
|
|
|
60
|
|
|
from.data('ajax-data-name', 'from'); |
61
|
|
|
to.data('ajax-data-name', 'to'); |
62
|
|
|
|
63
|
|
|
from |
64
|
|
|
.add(to) |
65
|
|
|
.on('keyup change', function () { |
66
|
|
|
table.draw(); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
if (from.closest('.input-date').length > 0 && to.closest('.input-date').length > 0) { |
70
|
|
|
from.closest('.input-date') |
71
|
|
|
.add(to.closest('.input-date')) |
72
|
|
|
.on('dp.change', function () { |
73
|
|
|
table.draw(); |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
isDateRange = true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
let checkDateRange = (from, to, value) => { |
80
|
|
|
let fromValue = from.val(), |
81
|
|
|
toValue = to.val(); |
82
|
|
|
|
83
|
|
|
if (fromValue != '') { |
84
|
|
|
fromValue = from.closest('.input-date').data('DateTimePicker').date(); |
85
|
|
|
} else { |
86
|
|
|
fromValue = undefined; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (toValue != '') { |
90
|
|
|
toValue = to.closest('.input-date').data('DateTimePicker').date(); |
91
|
|
|
} else { |
92
|
|
|
toValue = undefined; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
value = moment(value, from.data('date-format')); |
96
|
|
|
|
97
|
|
|
if(!_.isObject(fromValue) && !_.isObject(toValue)) { |
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!value.isValid()) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!_.isObject(fromValue) && value.isSameOrBefore(toValue)) { |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if(!_.isObject(toValue) && value.isSameOrAfter(fromValue)) { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return value.isBetween(fromValue, toValue) |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
let checkNumberRange = (from, to, value) => { |
117
|
|
|
let fromValue = parseInt(from.val()), |
118
|
|
|
toValue = parseInt(to.val()); |
119
|
|
|
|
120
|
|
|
value = parseInt(value); |
121
|
|
|
|
122
|
|
|
if(_.isNaN(fromValue) && _.isNaN(toValue)) { |
|
|
|
|
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if(_.isNaN(value)) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if(_.isNaN(fromValue) && value <= toValue) { |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if( _.isNaN(toValue) && value >= fromValue) { |
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return value >= fromValue && value <= toValue; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$.fn.dataTable.ext.search.push((settings, data, dataIndex) => { |
|
|
|
|
142
|
|
|
if (table.settings()[0].sTableId != settings.sTableId) { |
143
|
|
|
return true; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
let value = data[index]; |
147
|
|
|
|
148
|
|
|
if (value && !_.isUndefined(value['@data-order'])) { |
|
|
|
|
149
|
|
|
value = value['@data-order']; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (isDateRange) { |
153
|
|
|
return checkDateRange(from, to, value) |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return checkNumberRange(from, to, value) |
157
|
|
|
}); |
158
|
|
|
}, |
159
|
|
|
select (input, table, column, index) { |
|
|
|
|
160
|
|
|
var $input = $(input); |
161
|
|
|
|
162
|
|
|
$input.on('change', () => { |
163
|
|
|
let val = $input.val() ? $input.find(':selected').text() : ''; |
164
|
|
|
column.search(val).draw() |
165
|
|
|
}); |
166
|
|
|
}, |
167
|
|
|
text (input, table, column, index) { |
|
|
|
|
168
|
|
|
var $input = $(input) |
169
|
|
|
|
170
|
|
|
$input.on('keyup change', () => { |
171
|
|
|
column.search($input.val()).draw(); |
172
|
|
|
}) |
173
|
|
|
} |
174
|
|
|
} |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.